package com.hzw.controller;

import com.hzw.bean.User;
import com.hzw.dto.ApiResult;
import com.hzw.dto.IsReadParam;
import com.hzw.dto.MessageResponse;
import com.hzw.service.MessageService;
import com.hzw.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.hzw.api.Api.CLIENT_ERROR;
import static com.hzw.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/5.
 */
@Controller
@CrossOrigin
@RequestMapping(value = "/message")
public class MessageController {
    @Resource
    private MessageService messageService;

    /**
     * 通过用户Id获取属于此用户的所有消息
     * @param user
     * @return
     */
    @RequestMapping(value = "/getMessageByUserId",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getMessageByUserId(@RequestBody User user){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(user.getId())){
            return result.error(CLIENT_ERROR,"缺少参数:id");
        }else {
            try{
                MessageResponse messageResponseList = messageService.getMessageByUserId(user.getId());
                result.setData(messageResponseList);
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据用户ID和消息类型设置消息已读
     * @param param
     * @return
     */
    @RequestMapping(value = "/setMessageIsRead",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult setMessageIsRead(@RequestBody  IsReadParam param){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(param.getType()) || ObjectUtils.isNullOrEmpty(param.getUserId())){
            return result.error(CLIENT_ERROR,"缺少参数:type或userId");
        }else {
            messageService.setMessageIsRead(param);
        }
        return result;
    }
}
